<template>
  <div class="resident-list">
    <div class="page-header">
      <h2><i class="fas fa-users"></i> 老人列表</h2>
      <div class="search-box">
        <i class="fas fa-search"></i>
        <input type="text" v-model="searchQuery" placeholder="搜索老人姓名...">
      </div>
    </div>

    <div v-if="loading" class="loading-state">
      <i class="fas fa-spinner fa-spin"></i>
      <span>正在加载数据...</span>
    </div>

    <div v-else-if="error" class="error-state">
      <i class="fas fa-exclamation-circle"></i>
      <span>{{ error }}</span>
      <button @click="fetchResidents" class="retry-btn">
        <i class="fas fa-redo"></i> 重试
      </button>
    </div>

    <div v-else-if="!residents.length" class="empty-state">
      <i class="fas fa-inbox"></i>
      <span>暂无老人信息</span>
    </div>

    <div v-else class="table-container">
      <table>
        <thead>
          <tr>
            <th><i class="fas fa-user"></i> 姓名</th>
            <th><i class="fas fa-birthday-cake"></i> 年龄</th>
            <th><i class="fas fa-venus-mars"></i> 性别</th>
            <th><i class="fas fa-heartbeat"></i> 健康状况</th>
            <th><i class="fas fa-door-open"></i> 房间号</th>
            <th><i class="fas fa-cog"></i> 操作</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="resident in filteredResidents" :key="resident._id">
            <td>{{ resident.name }}</td>
            <td>{{ resident.age }}岁</td>
            <td>{{ resident.gender }}</td>
            <td>
              <span :class="['health-status', getHealthStatusClass(resident.healthStatus)]">
                {{ resident.healthStatus }}
              </span>
            </td>
            <td>{{ resident.roomNumber }}</td>
            <td class="actions">
              <button class="btn-edit" @click="$emit('edit-resident', resident)" title="编辑">
                <i class="fas fa-edit"></i>
              </button>
              <button class="btn-health" @click="$emit('view-health', resident)" title="健康记录">
                <i class="fas fa-notes-medical"></i>
              </button>
              <button class="btn-delete" @click="deleteResident(resident._id)" title="删除">
                <i class="fas fa-trash-alt"></i>
              </button>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
</template>

<script>
import axios from 'axios'

export default {
  name: 'ResidentList',
  data() {
    return {
      residents: [],
      searchQuery: '',
      loading: false,
      error: null
    }
  },
  computed: {
    filteredResidents() {
      return this.residents.filter(resident => 
        resident.name.toLowerCase().includes(this.searchQuery.toLowerCase())
      )
    }
  },
  async created() {
    await this.fetchResidents()
  },
  methods: {
    async fetchResidents() {
      this.loading = true
      this.error = null
      try {
        const response = await axios.get('/api/residents')
        if (response.data.success) {
          this.residents = response.data.data
        } else {
          throw new Error(response.data.message)
        }
      } catch (error) {
        this.error = `获取数据失败: ${error.response?.data?.message || error.message}`
        console.error('获取老人列表失败:', error)
      } finally {
        this.loading = false
      }
    },
    async deleteResident(id) {
      if (confirm('确定要删除这条记录吗？')) {
        try {
          const response = await axios.delete(`/api/delete-resident/${id}`)
          if (response.data.success) {
            await this.fetchResidents()
          } else {
            throw new Error(response.data.message)
          }
        } catch (error) {
          alert(error.response?.data?.message || '删除失败')
        }
      }
    },
    getHealthStatusClass(status) {
      const statusMap = {
        '良好': 'good',
        '一般': 'normal',
        '需要关注': 'warning',
        '需要护理': 'danger'
      }
      return statusMap[status] || 'normal'
    }
  }
}
</script>

<style scoped>
.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.page-header h2 {
  color: #1e88e5;
  display: flex;
  align-items: center;
  gap: 10px;
}

.search-box {
  position: relative;
}

.search-box i {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  color: #999;
}

.search-box input {
  padding: 8px 10px 8px 35px;
  border: 1px solid #ddd;
  border-radius: 20px;
  width: 250px;
  font-size: 0.9rem;
}

.table-container {
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  background: white;
}

th {
  background: #f8f9fa;
  padding: 12px;
  text-align: left;
  font-weight: 600;
  color: #444;
  border-bottom: 2px solid #eee;
}

th i {
  margin-right: 5px;
  color: #1e88e5;
}

td {
  padding: 12px;
  border-bottom: 1px solid #eee;
}

tr:hover {
  background-color: #f8f9fa;
}

.health-status {
  padding: 4px 8px;
  border-radius: 12px;
  font-size: 0.9rem;
}

.health-status.good {
  background-color: #e8f5e9;
  color: #2e7d32;
}

.health-status.normal {
  background-color: #e3f2fd;
  color: #1565c0;
}

.health-status.warning {
  background-color: #fff3e0;
  color: #ef6c00;
}

.health-status.danger {
  background-color: #ffebee;
  color: #c62828;
}

.actions {
  display: flex;
  gap: 8px;
}

.actions button {
  padding: 6px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.btn-edit {
  background-color: #e3f2fd;
  color: #1565c0;
}

.btn-health {
  background-color: #e8f5e9;
  color: #2e7d32;
}

.btn-delete {
  background-color: #ffebee;
  color: #c62828;
}

.actions button:hover {
  transform: translateY(-2px);
  filter: brightness(0.95);
}

@media (max-width: 768px) {
  .page-header {
    flex-direction: column;
    gap: 15px;
  }
  
  .search-box input {
    width: 100%;
  }
}

.loading-state,
.error-state,
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px;
  background: white;
  border-radius: 8px;
  text-align: center;
}

.loading-state i,
.error-state i,
.empty-state i {
  font-size: 2rem;
  margin-bottom: 1rem;
  color: #1e88e5;
}

.error-state i {
  color: #e53935;
}

.empty-state i {
  color: #9e9e9e;
}

.loading-state span,
.error-state span,
.empty-state span {
  color: #666;
  margin-bottom: 1rem;
}

.retry-btn {
  margin-top: 1rem;
  padding: 8px 16px;
  background-color: #1e88e5;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: all 0.3s ease;
}

.retry-btn:hover {
  background-color: #1976d2;
}

@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

.fa-spinner {
  animation: spin 1s linear infinite;
}
</style> 